package com.hairline.hairlinemarket.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.hairlinemarket.bean.MarketCategory;
import com.hairline.hairlinemarket.bean.MarketGoods;
import com.hairline.hairlinemarket.bean.MarketGoodsExample;
import com.hairline.hairlinemarket.bean.common.BasePageInfo;
import com.hairline.hairlinemarket.mapper.MarketCategoryMapper;
import com.hairline.hairlinemarket.mapper.MarketGoodsMapper;
import com.hairline.hairlinemarket.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.*;

/**
 * Author:杨小凯
 * Date:2023-04-20 18:46
 * Description:<描述>
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private MarketGoodsMapper goodsMapper;

    @Autowired
    private SystemService systemService;
    @Autowired
    private IssueService issueService;
    @Autowired
    private GoodsSpecificationService goodsSpecificationService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private GoodsProductService goodsProductService;
    @Autowired
    private  GoodsAttributeService goodsAttributeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private GrouponService grouponService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    CollectService collectService;

    @Override
    public List queryByNew(int start, Integer newLimit) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        // TODO 排序
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        ArrayList<MarketGoods> list = new ArrayList<>();
        for (int i = 0; i < newLimit; i++) {
            list.add(marketGoods.get(i));
        }
        return list;
    }

    @Override
    public List queryByHot(int start, Integer hotLimit) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        // TODO 排序
        criteria.andIsHotEqualTo(true);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        ArrayList<MarketGoods> list = new ArrayList<>();
        for (int i = 0; i < hotLimit; i++) {
            list.add(marketGoods.get(i));
        }
        return list;
    }

    @Override
    public List queryByCatId(Integer id) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        return marketGoods;
    }


    /*
    封装三个数据
    brotherCategory
    currentCategory
    parentCategory
     */
    @Override
    public Map<String, Object> selectById(Long id) {
        return null;
    }

    @Override
    public List<MarketGoods> queryByCategory(List<Integer> l2List, int start, Integer catlogMoreLimit) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdIn(l2List);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        ArrayList<MarketGoods> goods = new ArrayList<>();
        for (Integer i = start; i < catlogMoreLimit; i++) {
            goods.add(marketGoods.get(i));
        }

        return goods;
    }

    @Override
    public Map<String, Object> selectByGoodsId(Integer id) {
        // 查询商品
        MarketGoods info = goodsMapper.selectByPrimaryKey(id);
        // 用户是否收藏该商品
        int userHasCollect = collectService.selectReslute(id);

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 查询通用问题问题
        Callable<List> issueCallable = () -> issueService.queryCommon();
        // 该商品的评论
        Callable<Map> commentsCallable = () -> commentService.queryByGoodsId(id);
        // TODO 规格列表 goods_specification表 → 注意按照响应结果的格式来封装
        Callable<List> objectCallable = () -> goodsSpecificationService.queryByGoodsId(id);
        // 货品信息 goods_product表
        Callable<List> productListCallable = () -> goodsProductService.queryByGoodsId(id);
        // 参数信息 goods_attribute表
        Callable<List> goodsAttributeList = () -> goodsAttributeService.queryByGoodsId(id);
        // 制造商信息     根据goodsId查出对应的brandId,再到brand表中根据id查询
        Callable<List> brandCallable = () -> brandService.queryById(id);

        Callable<List> grouponCallable = () -> grouponService.queryById(id);

        FutureTask<List> grouponCallableTask = new FutureTask<>(grouponCallable);
        FutureTask<List> issueCallableTask = new FutureTask<>(issueCallable);
        FutureTask<Map> commentsCallableTask = new FutureTask<>(commentsCallable);
        FutureTask<List> objectCallableTask = new FutureTask<>(objectCallable);
        FutureTask<List> productListCallableTask = new FutureTask<>(productListCallable);
        FutureTask<List> goodsAttributeListTask = new FutureTask<>(goodsAttributeList);
        FutureTask<List> brandCallableTask = new FutureTask<>(brandCallable);

        executorService.submit(issueCallableTask);
        executorService.submit(commentsCallableTask);
        executorService.submit(objectCallableTask);
        executorService.submit(productListCallableTask);
        executorService.submit(goodsAttributeListTask);
        executorService.submit(brandCallableTask);
        executorService.submit(grouponCallable);
        Map<String, Object> data = new HashMap<>();
        // goods的信息
        data.put("info", info);

        data.put("groupon",grouponService.queryById(id));
        // 已经收藏该商品的用户数量
        data.put("userHasCollect", userHasCollect);
        // 后台管理中的通用问题
        try {
            data.put("issue", issueCallableTask.get());
            // 商品评论
            data.put("comment", commentsCallableTask.get());
            // 规格列表 goods_specification表 → 注意按照响应结果的格式来封装
            data.put("specificationList", objectCallableTask.get());
            // 货品信息 goods_product表
            data.put("productList", productListCallableTask.get());
            // 参数信息 goods_attribute表
            data.put("attribute", goodsAttributeListTask.get());
            // 制造商信息
            data.put("brand", brandCallableTask.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        // 是否分享
        data.put("share", systemService.isAutoCreateShareImage());
        // 商品分享图片地址
        data.put("shareImage", info.getShareUrl());
        return data;
    }

    @Override
    public MarketGoods findById(Integer id) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        if (marketGoods==null){
            return null;
        }
        return marketGoods.get(0);
    }

    @Override
    public PageInfo<MarketGoods> queryByCategoryPage(int cid, int i, int related) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        PageHelper.startPage(i+1, related);
        criteria.andCategoryIdIn(Arrays.asList(cid));
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> goodsPageInfo = new PageInfo<>(marketGoods);
        return goodsPageInfo;
    }

    @Override
    public Map<String, Object> selectCategory(Integer id) {

        HashMap<String, Object> map = new HashMap<>();

        List<MarketCategory> brother = categoryService.selectBrother(id);

        MarketCategory current = categoryService.selectCurrent(id);

        MarketCategory parent = categoryService.selectParent(id);

        map.put("brotherCategory", brother);
        map.put("currentCategory", current);
        map.put("parentCategory", parent);
        return map;
    }

    @Override
    public PageInfo<MarketGoods> selectGoodsList(Integer categoryId,Integer page,Integer limit) {

        PageHelper.startPage(page, limit);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.or().andCategoryIdEqualTo(categoryId);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
        return pageInfo;
    }

    @Override
    public Integer selectGoodsCount() {
        Integer count = categoryService.selectGoodsCount();
        return count;
    }


}
